'''
@Project ：dataProcessing.py 
@File    ：PortSelectDialog.py
@IDE     ：PyCharm 
@Author  ：子协
@Date    ：2025-03-25 20:19 
'''
from PyQt5.QtWidgets import QVBoxLayout,  QLabel,QDialogButtonBox, QDialog, QComboBox,QDesktopWidget
import serial.tools.list_ports

DIALOG_Height = 100 # 弹窗的大小（高）
DIALOG_Width = 100  # 弹窗的大小（宽）

# 串口选择窗口
class SerialPortSelectionDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("选择串口")

        # 获取所有可用串口
        self.ports = [port.device for port in serial.tools.list_ports.comports()]

        # 创建下拉框，添加串口列表
        self.combo_box = QComboBox()
        self.combo_box.addItems(self.ports)

        # 设置按钮布局
        self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)

        layout = QVBoxLayout()
        layout.addWidget(QLabel("请选择串口:"))
        layout.addWidget(self.combo_box)
        layout.addWidget(self.buttons)
        self.setLayout(layout)

    def get_selected_port(self):
        return self.combo_box.currentText()
    def center(self):
        screen = QDesktopWidget().screenGeometry()  # 获取屏幕的大小
        self.setGeometry((screen.width()-DIALOG_Width)//2, (screen.height()-DIALOG_Height)//2, DIALOG_Width, DIALOG_Height)